import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
const Login = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
const Home = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

// import Category from '../components/aside/goods/categories.vue'
// import Param from '../components/aside/goods/params.vue'
const Category = () => import( /* webpackChunkName: "category_param" */ '../components/aside/goods/categories.vue')
const Param = () => import( /* webpackChunkName: "category_param" */ '../components/aside/goods/params.vue')

// import Goods from '../components/aside/goods/goods.vue'
// import Add from '../components/aside/goods/Add.vue'
// import UpdateGoods from '../components/aside/goods/UpdateGoods.vue'
const Goods = () => import( /* webpackChunkName: "goods_add_update" */ '../components/aside/goods/goods.vue')
const Add = () => import( /* webpackChunkName: "goods_add_update" */ '../components/aside/goods/Add.vue')
const UpdateGoods = () => import( /* webpackChunkName: "goods_add_update" */ '../components/aside/goods/UpdateGoods.vue')

// import Order from '../components/aside/order/orders.vue'
// import Report from '../components/aside/stats/reports.vue'
const Order = () => import( /* webpackChunkName: "order_report" */ '../components/aside/order/orders.vue')
const Report = () => import( /* webpackChunkName: "order_report" */ '../components/aside/stats/reports.vue')

// import Right from '../components/aside/authority/rights.vue'
// import Role from '../components/aside/authority/roles.vue'
// import User from '../components/aside/user/users.vue'
const User = () => import( /* webpackChunkName: "user_role_right" */ '../components/aside/user/users.vue')
const Role = () => import( /* webpackChunkName: "user_role_right" */ '../components/aside/authority/roles.vue')
const Right = () => import( /* webpackChunkName: "user_role_right" */ '../components/aside/authority/rights.vue')



const routes = [{
    path: '/',
    name: 'index',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        name: 'welcome',
        component: Welcome
      },
      {
        path: '/categories',
        name: 'category',
        component: Category
      },
      {
        path: '/goods',
        name: 'goods',
        component: Goods
      },
      {
        path: '/goods/add',
        name: 'add',
        component: Add
      },
      {
        path: '/goods/update',
        name: 'updateGoods',
        component: UpdateGoods
      },
      {
        path: '/orders',
        name: 'order',
        component: Order
      },
      {
        path: '/params',
        name: 'param',
        component: Param
      },
      {
        path: '/reports',
        name: 'repost',
        component: Report
      },
      {
        path: '/rights',
        name: 'right',
        component: Right
      },
      {
        path: '/roles',
        name: 'role',
        component: Role
      },
      {
        path: '/users',
        name: 'user',
        component: User
      }
    ]
  }
]


Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫,实现权限控制

router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  // 如果用户访问登录路径，直接放行
  if (to.path === '/login' || to.path === '/') return next()
  // 如果用户访问非登录路径，则判断有无登录凭证token
  let loginToken = sessionStorage.getItem('token')
  // 没有token，直接跳转登录页面
  if (!loginToken) return next('/login')
  // 有token，放行
  next()
})

export default router
